<?php
/**
 * Created by PhpStorm.
 * User: august
 * Date: 2021/2/23
 * Time: 17:36
 */

namespace common\models;



use common\libs\CacheKey;
use think\facade\Cache;

class MenuModel extends BaseModel
{
    protected $table = 'menu';

    /**
     * 获取菜单html
     * author: august 2021/3/3
     * @return mixed|string
     */
    public static function getMenus()
    {
        $cacheKey = CacheKey::KEY_MENU_LIST_HTML;
        if ($menus = Cache::get($cacheKey)) {
            return $menus;
        }
        $menus = [];
        foreach (self::getList() as $row) {
            $menus[$row['parent_id']][] = $row;
        }
        $menuHtml = '';
        foreach ($menus[0] ?? [] as $row) {
            $menuHtml .=self::_renderMenu($row, $menus, true);
        }
        Cache::set($cacheKey, $menuHtml);
        return $menuHtml;
    }


    private static function _renderMenu($row, $menus)
    {
        $icon = '';
        if (!empty($row['data'])) {
            $data = json_decode($row['data'], true);
            if (!empty($data['icon'])) {
                $icon = 'fa ' . $data['icon'];
            }
        }
        if (empty($menus[$row['id']])) {    //没有子菜单
            $href = $row['route'] === '/' ? 'javascript:void(0);' : url($row['route'])->build();
            return <<<HTML
            <li>
                <a href="$href">
                    <i class="$icon"></i> <span>{$row['name']}</span>
                </a>
            </li>
HTML;
        } else {
            $html = '<li class="treeview">
                        <a href="javascript:void(0);">
                            <i class="'. $icon .'"></i>
                            <span>' . $row['name'] . '</span>
                            <i class="fa fa-angle-left pull-right"></i>
                        </a>';
            $html .= '<ul class="treeview-menu">';
            foreach ($menus[$row['id']] as $_row) {
                $html .= self::_renderMenu($_row, $menus);
            }
            $html .= '</ul>';
        }
        return $html;
    }

    public static function getList($pageData = [])
    {
        // todo 缓存
        $query = static::where('id', '>', 0);
        $query->hidden(['create_time', 'update_time']);
        if ($pageData) {
            if(!empty($pageData['sort']) && !empty($pageData['order'])) {
                $query->order($pageData['sort'], $pageData['order']);
            }
            if (!empty($pageData['page']) && !empty($pageData['pageSize'])) {
                $query->page($pageData['page'], $pageData['pageSize']);
            }
        }
        $list = $query->select();
        if ($list->isEmpty()) {
            return [];
        }

        return $list->toArray();
    }

    public static function getSubIds($id, $withSelf = false)
    {
        $res = $withSelf ? [$id] : [];
        $list = self::getList();
        if (!$list) {
            return $res;
        }
        $idMapList = $pidMapList = [];
        foreach ($list as $row) {
            $idMapList[$row['id']] = $row;
            $pidMapList[$row['parent_id']] = $row;
        }
        if (empty($pidMapList[$id])) {
            return $res;
        } else {
            return array_merge($res, self::_getSubIds($pidMapList[$id] ? [$pidMapList[$id]] : []));
        }
    }

    private static function _getSubIds($pidMapList)
    {
        $ids = [];
        if (empty($pidMapList)) {
            return [];
        }
        foreach ($pidMapList as $row) {
            array_push($ids, $row['id']);
            $subList = $pidMapList[$row['id']] ?? [];
            $ids = array_merge($ids, self::_getSubIds($subList ? [$subList] : []));
        }
        return $ids;
    }
}